/*
 * @Descripttion: 
 * @version: 
 * @Author: 
 * @Date: 2022-04-24 09:01:49
 * @LastEditors: weidelong
 * @LastEditTime: 2022-05-20 14:29:11
 */
import { createStore } from 'vuex'
import createPersistedstate from 'vuex-persistedstate'

export default createStore({
    plugins: [
        createPersistedstate({
          storage: window.sessionStorage,
          key: 'saveInfo',
          paths: ['articleRemind', 'username', 'isLogin', 'chatType', 'myInfo', 'userInfo', 'userList', 'chatMessageList']
        })
      ],
  state: {
    username: {
      name: ''
    },
    isLogin: JSON.parse(localStorage.getItem('isLogin')) || false,//默认的登录状态,


    // 聊天类型
    chatType: '',
    // 用户离线
    userOut: null,

    // 文章提醒
    articleRemind: [],
    
    // 我都信息
    myInfo: {
        img: '',
        name: ''
    },
    // 别人的信息（聊天对象）
    userInfo: {
        img: '',
        name: ''
    },
    // 用户列表
    userList: [],
    // 聊天记录
    chatMessageList: [],
  },
  mutations: {
      setUserInfo(state, data) {
        state.userInfo = data
      },
      setMyInfo(state, data) {
        state.myInfo = data
        state.isLogin = true
      },
      /*渲染用户列表 */
      SOCKET_login(state, data) {
        state.userList = data
      },
      // 聊天记录更新
      SOCKET_updateChatMessageList(state, data) {
        state.chatMessageList.push(data)
      },
      // 修改聊天类型
      changeChatType(state, data) {
        state.chatType = data
      },
      // 文章提醒
      setArticleRemind(state, data) {
        if (state.articleRemind.length >= 3) {
          state.articleRemind.shift()
          state.articleRemind.push(data.name)
        } else {
          state.articleRemind.push(data.name)
        }
      },
      deleteArticleRemindElement(state, index) {
        state.articleRemind.splice(index, 1)
      },
      // 用户离线
      SOCKET_outLine(state, data) {
        state.userOut = data.a
    },


      
    SET_USER_INFO(state, data){
      state.username = data
    },
  },
  actions: {},
  modules: {}
})
